/* Copyright 2009 Requirement Management System
 *  
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.dao.provider;

import java.io.Serializable;
import java.util.Collection;

import org.mags.dao.common.EntityProperties;
import org.mags.dao.common.Limits;
import org.mags.dao.common.NoCacheableDataProvider;
import org.mags.dao.common.SOrder;
import org.mags.dao.common.RestrictionAttribute;
import org.mags.dao.common.RestrictionBuilder;
import org.mags.dao.common.Attribute;

/**
 * 
 * @author Mario Gaitan
 *
 */
public abstract class AbstractNoCacheableDataProvider<T> 
	extends AbstractDataProvider<T> 
		implements NoCacheableDataProvider<T> {
	private static final long serialVersionUID = 1L;
	
	@Override
	public Number count() {
		return super.count(null, null);
	}
	protected boolean existsWithRestriction(EntityProperties entityProperties, RestrictionBuilder restriction) {
		return super.existsWithRestriction(entityProperties, restriction);
	}		
	protected Number executeUpdate( String query, Attribute[] attributes) {
		return super.executeUpdate(query, attributes);
	}
	/*READS*/
	@Override
	public T getById(Serializable id) {
		T one = super.getById(id);
		if(one!=null)
			loadProperties(one);
		return one;
	}	
	protected Object getObject( String query,  Attribute[] attributes,  Limits limits) {
		return super.executeQuery(query, attributes, limits);
	}
	protected Collection executeQuery( String query,  Attribute[] attributes,  Limits limits) {
		return  super.executeQuery(query, attributes, limits);
	}
	protected Collection<T> getByRestriction(EntityProperties entityProperties, RestrictionBuilder restriction, SOrder[] orderBy, Limits limits) {
		Collection<T> many = super.getByRestriction(entityProperties, restriction, orderBy, limits);
		loadProperties(many);
		return many;		
	}
	protected T getOneByRestriction(RestrictionBuilder restriction) {
		T one = super.getOneByRestriction(restriction);
		if(one!=null)
			loadProperties(one);
		return one;
	}
	protected Collection<T> getByAttribute(RestrictionAttribute attribute, SOrder[] orderBy, Limits limits) {
		Collection<T> many = super.getByAttribute(attribute, orderBy, limits);
		loadProperties(many);
		return many;			
	}
	protected T getOneByAttribute(RestrictionAttribute attribute) {
		T one = super.getOneByAttribute(attribute);
		if(one!=null)
			loadProperties(one);
		return one;	
	}
	protected void loadProperties(Collection<T> all) {
		for(T i: all)
			loadProperties(i);
	}	
	/**
	 * implements when need load properties to objects that this is getting
	 * @param one: object to load properties
	 */	
	protected void loadProperties(T one) {
		//DO NOTHING
	}
}
